package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrOperatingResultsForWebsiteBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.manage.OperatingResultsAdapter;
import com.sgb.kjwl.viewmodel.manage.OperatingResultsForWebsiteModel;
import com.sgb.member.entity.OperatingResultsEntity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/*
 * Author LiWei
 * Create Time 2021/4/15
 * Description 网站-经营业绩
 * */
public class OperatingResultsForWebsite extends AppLazyFragment {

    @Inject
    OperatingResultsForWebsiteModel viewModel;
    private FrOperatingResultsForWebsiteBinding binding;
    private OperatingResultsAdapter adapter;

    private String websiteNo;
    private int websiteType;

    public static OperatingResultsForWebsite newInstance(String websiteNo, int websiteType) {
        OperatingResultsForWebsite fragment = new OperatingResultsForWebsite();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putInt("websiteType", websiteType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_operating_results_for_website, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getInt("websiteType");
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        adapter = new OperatingResultsAdapter(getActivity(), null);
        adapter.setOnItemClick(new AdapterOnItemClick<OperatingResultsEntity>() {
            @Override
            public void onItemClick(OperatingResultsEntity entity, int position) {
                viewModel.getH5Url(entity.getId());
            }
        });
        binding.listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.listView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp10)));
        binding.listView.setAdapter(adapter);
        binding.listView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getOperatingResultsList(websiteNo, websiteType, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getOperatingResultsList(websiteNo, websiteType, true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getOperatingResultsList(websiteNo, websiteType, false);
    }

    public void showList(List<OperatingResultsEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.listView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.listView.setNoMore(isLast);
        } else {
            binding.listView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }
}


